/**
 * 
 */
package android.coocoo.views;

import java.util.ArrayList;
import java.util.Calendar;
import android.content.Intent;
import android.coocoo.common.Const;
import android.coocoo.common.TasksListAdapter;
import android.coocoo.common.Util;
import android.coocoo.common.XmlParser;
import android.coocoo.models.CategoryInfo;
import android.coocoo.models.TaskInfo;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author ThuyTrang
 *
 */
public class DisplayTasksListByDateActivity extends ListActivity{
	
	ArrayList<TaskInfo> tasksList;
	
	private static int DATE_DIALOG = 0;
	
	int year;
	int month;
	int dayOfMonth;
	ArrayList<TaskInfo> _tasksList = null;
	ArrayList<CategoryInfo> categoriesList;
	
	TextView btGetDate;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_tasks_list_bydate);
        this.tasksList = getIntent().getParcelableArrayListExtra("tasksList");
        this.categoriesList = getIntent().getParcelableArrayListExtra("categoriesList");
//        Log.e("pass_flag", "Intent created");
        
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        btGetDate = (TextView)findViewById(R.id.btGetDate);
        btGetDate.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(DATE_DIALOG);
			}
		});
        
        updateDisplay(year, month, dayOfMonth);
    }

	@Override
	protected Dialog onCreateDialog(int id) {
	    if (id == DATE_DIALOG){
//	   		Log.e("data", task.getStartTime().getYear() + "/" + task.getStartTime().getMonth() + "/" + task.getStartTime().getDate());
	   		return new DatePickerDialog(this, mDateSetListener, year, month, dayOfMonth);
	    }
	    return null;
	}
	
	private DatePickerDialog.OnDateSetListener mDateSetListener =
		new DatePickerDialog.OnDateSetListener() {
			public void onDateSet(DatePicker view, int _year, int _monthOfYear, int _dayOfMonth) {
				year = _year;
				month = _monthOfYear;
				dayOfMonth = _dayOfMonth;
				updateDisplay(year, month, dayOfMonth);
			}
		};
		
	private void updateDisplay(int year, int month, int dayOfMonth){
		
		// Rearrange tasksList
		ArrayList<TaskInfo> _tasksList = getTasksListByDate(year, month, dayOfMonth);
		Util.reArrangeTasksList(_tasksList);
		btGetDate.setText(dayOfMonth + "/" + (month+1) + "/" + year);
		TasksListAdapter tasksListAdapter = new TasksListAdapter(this, _tasksList);
		tasksListAdapter.setByDate(true);
		setListAdapter(tasksListAdapter);
	    registerForContextMenu(getListView());
	}
	
	private ArrayList<TaskInfo> getTasksListByDate(int year, int month, int dayOfMonth){
		int i;
		if (tasksList!= null){
			_tasksList = new ArrayList<TaskInfo>();
			for (i=0; i<tasksList.size(); i++){
				if (tasksList.get(i).getStartTime().getYear() == year)
					if (tasksList.get(i).getStartTime().getMonth() == month)
						if (tasksList.get(i).getStartTime().getDate() == dayOfMonth){
//							if (_tasksList == null)
//								_tasksList = new ArrayList<TaskInfo>();
							_tasksList.add(tasksList.get(i));
						}
			}
		}
		return _tasksList;
	}
	
	@Override
	protected void onResume() {
		this.tasksList = getIntent().getParcelableArrayListExtra("tasksList");
		updateDisplay(year, month, dayOfMonth);
		super.onResume();
	}
	
	public void onListItemClick(ListView parent, View v, int position, long id) {
//		selection.setText(items[position]);
		Intent intent = new Intent(DisplayTasksListByDateActivity.this, DisplayTaskInfoActivity.class);
		TaskInfo _taskInfo = _tasksList.get(position);
		intent.putExtra("task", _taskInfo);
		intent.putExtra("categoriesList", categoriesList);
		startActivityForResult(intent, Const.Request.EDIT_TASK);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	  super.onCreateContextMenu(menu, v, menuInfo);
	  MenuInflater inflater = getMenuInflater();
	  inflater.inflate(R.menu.context_task_info, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	    // Handle item selection
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
	    switch (item.getItemId()) {
	    case R.id.edit_task:
	    	System.out.print("Choose edit on context menu");
	    	Intent intent = new Intent(DisplayTasksListByDateActivity.this, DisplayTaskInfoActivity.class);
			TaskInfo _taskInfo = _tasksList.get(info.position);
			intent.putExtra("task", _taskInfo);
			intent.putExtra("categoriesList", categoriesList);
			startActivityForResult(intent, Const.Request.EDIT_TASK);
	        return true;
	    case R.id.delete_task:
	    	System.out.print("Choose delete on context menu");
	    	int id = _tasksList.get(info.position).getId();
	    	int i;
	    	for (i=0; i<tasksList.size(); i++)
	    		if (tasksList.get(i).getId() == id){
	    			tasksList.remove(i);
	    			updateDisplay(year, month, dayOfMonth);
	    		}
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == Const.Request.EDIT_TASK)
        	if (resultCode == Const.Result.SUCCESS){
        		TaskInfo _taskInfo = intent.getParcelableExtra("returnTask");
        		int i;
        		for (i=0; i<tasksList.size(); i++)
        			if (tasksList.get(i).getId() == _taskInfo.getId()){
        				tasksList.remove(i);
        				tasksList.add(i, _taskInfo);
        			}
        		XmlParser xmlParser = new XmlParser();
        		xmlParser.setTasksList(tasksList);
        	}
	}
}
